package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author kk
 * #date 2024/6/23 14:07
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    SystemUserService systemUserService;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public CaptchaVo getCaptcha() {
        //验证码图片格式
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        //获取验证码的值
        String verCode = specCaptcha.text().toLowerCase();
        //将验证码转成图片格式
        String img = specCaptcha.toBase64();
        //把验证码保存到redis
        String codeKey = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID().toString();
        //验证码有效时间
        redisTemplate.opsForValue().set(codeKey,verCode,180, TimeUnit.SECONDS);

        // 将key和base64返回给前端
        return new CaptchaVo(img, codeKey);
    }

    @Override
    public String login(LoginVo loginVo) {

        //判断验证码
        if (!StringUtils.hasText(loginVo.getCaptchaCode())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }

        Object redisCode = redisTemplate.opsForValue().get(loginVo.getCaptchaKey());

        //判断验证码是否过期
        if (redisCode == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }

        //判断验证码是否正确
        if (!loginVo.getCaptchaCode().equals(redisCode)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }

        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemUser::getUsername, loginVo.getUsername());

        SystemUser user = systemUserService.getOne(queryWrapper);

        // SystemUser user = systemUserService.getOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, loginVo.getUsername()));
        //账号是否存在
        if (user == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }

        //账号是否被禁用
        if (user.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }

        //验证密码
        String pwd = DigestUtils.md5Hex(loginVo.getPassword());
        if (!user.getPassword().equals(pwd)){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }

        //登录成功，创建token，生成令牌
        String token = JwtUtil.createToken(user.getUsername(), user.getId());

        return token;
    }
}
